using UnityEngine;
using System.Collections;

public class Cronometro : MonoBehaviour 
{
	private int id = 5;
	private Vector2 screen;
	
	public bool start;
	
	void Start () 
	{
		this.gameObject.AddComponent("MeshRenderer");
		this.gameObject.AddComponent("MeshFilter");
		
		this.screen.y = GameObject.Find("Main Camera").GetComponent<Camera>().orthographicSize;
		this.screen.x = this.screen.y * GameObject.Find("Main Camera").GetComponent<Camera>().aspect;
		
        Mesh mesh = Quad.Create(2.0f, 2.0f);
		
		this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
		this.gameObject.GetComponent<MeshRenderer>().material = AtlasJogo.globalMaterial;
		
		this.transform.position = new Vector3(0, 1, -1);
		this.StartCoroutine(Comece());
	}
	
	void Update () 
    {
		this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasJogo.rects[id].xMin, AtlasJogo.rects[id].yMax),
			new Vector2(AtlasJogo.rects[id].xMax, AtlasJogo.rects[id].yMax),
			new Vector2(AtlasJogo.rects[id].xMax, AtlasJogo.rects[id].yMin),
			new Vector2(AtlasJogo.rects[id].xMin, AtlasJogo.rects[id].yMin)
		};
	}
	
	IEnumerator Comece()
	{
		if(this.id > 8)
		{
			this.start = true;
			this.transform.position = new Vector3(0,0,-15);
			this.id = 8;
			StopAllCoroutines();
		}
		
		yield return new WaitForSeconds(1);
			
		this.id++;
		
		this.StartCoroutine(Comece());
	}
}
